//用于监听某个属性变化。
import Vue from 'vue'

export function watch(source, callback) {
  const vm = new Vue({
    data(){
      return {
        source
      }
    },
    watch: {
      // 如果 source 是对象，监听整个对象的 deep 变化
      source: {
        handler(val) {
          callback(val)
        },
        deep: true,
        immediate: true
      }
    },
    created() {
      // 把 source 绑定为 data.data
      this.data = source
    }
  })
  return () => vm.$destroy() // 返回取消监听函数
}
export function getResizeObserve(){
  const debounce = (fn, delay) => {
    let timer
    return (...args) => {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        fn(...args)
      }, delay)
    }
  }
  const _ResizeObserver = window.ResizeObserver;
  let ro = class ResizeObserver extends _ResizeObserver{
    constructor(callback) {
      callback = debounce(callback, 200);
      super(callback);
    }
  }
  return ro;
}